package com.haitaoit.chengquan.module.home.adapter.forum;

import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.ui.equipment.activity.ShowPictureActivity;
import com.haitaoit.chengquan.view.Img;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/21.
 */
public class ForumCenterImgAdapterTwo extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<Img> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public ForumCenterImgAdapterTwo(Context context) {
        this.context = context;
    }

    public List<Img> getList() {
        return list;
    }

    public void setList(List<Img> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size() > 3 ? 3 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.image_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DisplayMetrics metrics = convertView.getResources().getDisplayMetrics();

        ViewGroup.LayoutParams lp1 = holder.icon.getLayoutParams();
        lp1.height = (metrics.widthPixels - 50) / 3;

        holder.icon.setLayoutParams(lp1);
        ImageLoader.getInstance().displayImage(list.get(position).getImg_url(), holder.icon);
        holder.icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, ShowPictureActivity.class);
                intent.putExtra("image_uri", list.get(position).getImg_url());
                context.startActivity(intent);

            }
        });


        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.icon)
        ImageView icon;
    }
}
